home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / liboctave / oct-alloc.cc < prev    next >
C/C++ Source or Header  |  1996-11-07  |  2KB  |  111 lines

  1. /*
  2.  
  3. Copyright (C) 1996 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #if defined (__GNUG__)
  24. #pragma implementation
  25. #endif
  26.  
  27. #ifdef HAVE_CONFIG_H
  28. #include <config.h>
  29. #endif
  30.  
  31. #include <new>
  32.  
  33. #include "oct-alloc.h"
  34.  
  35. void *
  36. octave_allocator::alloc (size_t size)
  37. {
  38.   if (size != item_size)
  39.     return ::new char [size];
  40.  
  41.   if (! head)
  42.     {
  43.       if (! grow ())
  44.     return 0;
  45.     }
  46.  
  47.   link *tmp = head;
  48.   head = head->next;
  49.   return tmp;
  50. }
  51.  
  52. void
  53. octave_allocator::free (void *p, size_t size)
  54. {
  55.   if (size != item_size)
  56.     ::delete [] ((char *) p);
  57.   else
  58.     {
  59.       link *tmp = (link *) p;
  60.       tmp->next = head;
  61.       head = tmp;
  62.     }
  63. }
  64.  
  65. // Return TRUE for successful allocation, FALSE otherwise.
  66.  
  67. bool
  68. octave_allocator::grow (void)
  69. {
  70.   bool retval = true;
  71.  
  72.   char *start = new char [grow_size * item_size];
  73.  
  74.   if (start)
  75.     {
  76.       char *last = &start[(grow_size - 1) * item_size];
  77.  
  78.       char *p = start;
  79.       while (p < last)
  80.     {
  81.       char *next = p + item_size;
  82.       ((link *) p) -> next = (link *) next;
  83.       p = next;
  84.     }
  85.  
  86.       ((link *) last) -> next = 0;
  87.  
  88.       head = (link *) start;
  89.     }
  90.   else
  91.     {
  92.       typedef void (*error_handler_function) (void);
  93.  
  94.       error_handler_function f = set_new_handler (0);
  95.       set_new_handler (f);
  96.  
  97.       if (f)
  98.     f ();
  99.  
  100.       retval = false;
  101.     }
  102.  
  103.   return retval;
  104. }
  105.  
  106. /*
  107. ;;; Local Variables: ***
  108. ;;; mode: C++ ***
  109. ;;; End: ***
  110. */
  111.